/*
  学习目标：数据存入redux, 渲染界面
*/

import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getNewsByIdAction } from '../store/action/news';

import imgSrc from '../assets/defaultCover.jpg';

export default function News() {
  const dispatch = useDispatch();

  const { activeId } = useSelector((state) => state.channel);
  // 4. useSelector取出数据
  const { newsList } = useSelector((state) => state.news);
  useEffect(() => {
    dispatch(getNewsByIdAction(activeId));
    // 6. 使用activeId作为依赖项， 切换数据
  }, [dispatch, activeId]);
  return (
    <div className="list">
      {newsList.map((item) => {
        // 5. 列表渲染
        return (
          <div key={item.art_id} className="article_item">
            <h3 className="van-ellipsis">{item.title}</h3>
            <div className="img_box">
              <img
                src={item.cover.type !== 0 ? item.cover.images[0] : imgSrc}
                className="w100"
                alt=""
              />
            </div>
            <div className="info_box">
              <span>{item.aut_name}</span>
              <span>{item.comm_count}评论</span>
              <span>{item.pubdate}</span>
            </div>
          </div>
        );
      })}
    </div>
  );
}
